
from models import *
from app import app


# 下面是写入DB的初始化订单的操作，此时订单状态是未支付
def init_insert_order(order_info):
    with app.app_context():
        order_id = order_info["order_id"]
        user_id = order_info['user_id']
        passenger_card = order_info['passenger_card']
        train_no = order_info['train_no']
        carriage_no = order_info['carriage_no']
        arrive_day = order_info['arrive_day']
        seat_no = order_info['seat_no']
        price = order_info['price']
        create_time = order_info['create_time']
        start_station_name = order_info['start_station_name']
        start_station_no = order_info['start_station_no']
        end_station_name = order_info['end_station_name']
        end_station_no = order_info['end_station_no']
        order = Orders(user_id=user_id, passenger_card=passenger_card, train_no=train_no, carriage_no=carriage_no,
                       seat_no=seat_no, price=price, create_time=create_time, status='未支付', arrive_day=arrive_day,
                       start_station_name=start_station_name, start_station_no=start_station_no,
                       end_station_no=end_station_no, end_station_name=end_station_name, id=order_id)
        try:
            db.session.add(order)
            print("插入成功")
            db.session.commit()
            return True
        except Exception as e:
            print(e)
            return False
        finally:
            pass


# 下面是修改订单的状态为超时
def init_overtime_order(order_info):
    with app.app_context():
        order_id = order_info["order_id"]
        try:
            Orders.query.filter_by(id=order_id).update({'status': '已超时'})
            db.session.commit()
            return True
        except Exception as e:
            print(e)
            return False
        finally:
            pass


# 下面为修改订单状态为已支付
def init_paid_order(order_info):
    with app.app_context():
        order_id = order_info["order_id"]
        try:
            Orders.query.filter_by(id=order_id).update({'status': '已付款'})
            db.session.commit()
            return True
        except Exception as e:
            print(e)
            return False
        finally:
            pass
